using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Media;
using MauiBuilder.Services;
using MauiBuilder.ViewModels;
using MauiBuilder.Views;
using Microsoft.Extensions.Logging;
using ZXing.Net.Maui.Controls;

namespace MauiBuilder
{
    /// <summary>
    /// 应用程序入口类，负责配置和初始化MAUI应用程序
    /// </summary>
    public static class MauiProgram
    {
        /// <summary>
        /// 创建并配置MAUI应用程序
        /// </summary>
        /// <returns>配置好的MauiApp实例</returns>
        public static MauiApp CreateMauiApp()
        {
            // 初始化全局错误处理
            ErrorHandlingService.Initialize();
            // 创建应用程序构建器
            var builder = MauiApp.CreateBuilder();
            // 配置应用程序
            builder.UseMauiApp<App>()// 配置字体
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            })// 使用CommunityToolkit
            .UseMauiCommunityToolkit().UseBarcodeReader(); // 使用ZXing条码扫描

            // 注册视图和视图模型
            RegisterViewsAndViewModels(builder.Services);
            // 注册服务
            RegisterServices(builder.Services);
            // 配置日志
            ConfigureLogging(builder);
            // 构建并返回应用程序
            return builder.Build();
        }

        /// <summary>
        /// 注册视图和视图模型
        /// </summary>
        /// <param name = "services">服务集合</param>
        private static void RegisterViewsAndViewModels(IServiceCollection services)
        {
            //services.AddKeyedSingleton<ISpeechToText, SpeechToTextImplementation>("Online");
            //services.AddKeyedSingleton<ISpeechToText, OfflineSpeechToTextImplementation>("Offline");
            // 注册语音识别服务
            services.AddSingleton<ISpeechToText>(SpeechToText.Default);
            services.AddSingleton<ITextToSpeech>(TextToSpeech.Default);
            // 主页面（不再需要前台服务依赖）
            services.AddSingleton<MainPage, MainPageViewModel>();
            // 登录页面及视图模型
            services.AddSingleton<LoginPage, LoginViewModel>();
            // 注册页面及视图模型
            services.AddSingleton<RegisterPage, RegisterViewModel>();
            // 设置页面及视图模型
            services.AddSingleton<SettingsPage, SettingsViewModel>();
            // 关于页面及视图模型
            services.AddSingleton<AboutPage, AboutViewModel>();
            // 通知消息页面及视图模型
            services.AddSingleton<NotificationPage, NotificationViewModel>();
            // 前台服务页面及视图模型
            services.AddSingleton<ForegroundServicePage, ForegroundServiceViewModel>();
            // Socket连接管理页面及视图模型
            services.AddSingleton<SocketPage, SocketViewModel>();
            // 日志查看页面及视图模型
            services.AddSingleton<LogPage, LogPageViewModel>();
            // 设备信息页面及视图模型
            services.AddSingleton<DeviceInfoPage, DeviceInfoViewModel>();
            // 混合Web视图页面及视图模型
            services.AddSingleton<HybridWebPage, HybridWebViewModel>();
            // 功能测试页面及视图模型
            services.AddSingleton<FeatureTestPage, FeatureTestViewModel>();
            // 支付页面及视图模型
            services.AddSingleton<PaymentPage, PaymentViewModel>();
            // 扫码页面
            services.AddTransient<BarcodeScanPage>();
            // 聊天页面及视图模型
            services.AddSingleton<ChatPage, ChatViewModel>();
            // 语音转文本页面及视图模型
            services.AddSingleton<SpeechToTextPage, SpeechToTextViewModel>();
            // 记录视图注册日志
            LogManager.Instance.LogInfo("Views and ViewModels registered successfully");
        }

        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name = "services">服务集合</param>
        private static void RegisterServices(IServiceCollection services)
        {
            // 注册日志管理器为单例服务
            services.AddSingleton<LogManager>(_ => LogManager.Instance);
            // 注册主题管理器
            services.AddSingleton<ThemeManager>(_ => ThemeManager.Instance);
            // 注册支付管理服务
            services.AddSingleton<IPaymentManagerService, PaymentManagerService>();
            // 注册前台服务
#if ANDROID
            services.AddSingleton<IForegroundService, Platforms.Android.ForegroundServicePlatform>();
            services.AddSingleton<INotificationService, Platforms.Android.NotificationServicePlatform>();
            services.AddSingleton<IAppRestartService, Platforms.Android.AppRestartService>();
#elif IOS
            services.AddSingleton<IForegroundService, Platforms.iOS.ForegroundServicePlatform>();
            services.AddSingleton<INotificationService, Platforms.iOS.NotificationServicePlatform>();
            services.AddSingleton<IAppRestartService, Platforms.iOS.AppRestartService>();
#elif WINDOWS
            services.AddSingleton<IForegroundService, Platforms.Windows.ForegroundServicePlatform>();
            services.AddSingleton<INotificationService, Platforms.Windows.NotificationServicePlatform>();
            services.AddSingleton<IAppRestartService, Platforms.Windows.AppRestartService>();
#elif MACCATALYST
            services.AddSingleton<IForegroundService, Platforms.MacCatalyst.ForegroundServicePlatform>();
            services.AddSingleton<INotificationService, Platforms.MacCatalyst.NotificationServicePlatform>();
#endif
            // 记录服务注册日志
            LogManager.Instance.LogInfo("Services registered successfully");
        }

        /// <summary>
        /// 配置日志
        /// </summary>
        /// <param name = "builder">应用程序构建器</param>
        private static void ConfigureLogging(MauiAppBuilder builder)
        {
#if DEBUG
            // 在调试模式下添加调试日志
            builder.Logging.AddDebug();
            builder.Services.AddLogging(configure => configure.AddDebug());
            LogManager.Instance.LogInfo("Debug logging enabled");
            builder.Services.AddHybridWebViewDeveloperTools();
#endif
            // 清理过期日志
            LogManager.Instance.CleanupOldLogs(7);
        }
    }
}